#include <lvgl.h>
#include <TFT_eSPI.h>
#include <Ticker.h>//定时器

//lv_conf.h    文件里使用tft库打开， 使用之前配置好  tft库  使用的屏幕，以及SPI相关引脚
#include"AdKeyboard_manager.h"
#include"ui.h"
#include"tian_qi.h"



// 定义定时器对象
Ticker timer;//定时器



//时间处理
int a_year = 2024;
int a_month = 07;
int a_day = 11;
int a_hour = 15;
int a_minute = 55;
int a_second = 55;
int all_time[14];


//校准时间
int new_time(){
  int chi_shu = 0;
  int ct = 1;
  while(de_time2=="null" && chi_shu <=10){
      get_time();
      delay(10);
      chi_shu++;
  }
  if(chi_shu==11) { /* 获取时间失败  *//*fan_hui = "时间校准失败请检查网络连接"; */ ct = 0 ;goto go;}
  for(int i = 0; i <=14 ; i++){
  all_time[i]=char(de_time1[i])-'0';//将字符变为整数
  } 

a_year = all_time[0]*1000+all_time[1]*100+all_time[2]*10+all_time[3];
a_month = all_time[4]*10+all_time[5];
a_day = all_time[6]*10+all_time[7];
a_hour = all_time[8]*10+all_time[9];
a_minute = all_time[10]*10+all_time[11];
a_second = all_time[12]*10+all_time[13];
go:
de_time2="null";
return ct;
}



//定时器执行函数
//时间显示时钟计算，1秒钟调用一次
//时间刷新函数
void flash(int tt) {
  a_second++;
  if(a_second==60){
    a_minute++;
    a_second=0;
  }
   if(a_minute==60){
    a_hour++;
    a_minute=0;
  }
    if(a_hour==24){
    a_day++;
    a_hour=0;
  }
   switch(a_month){
   case 1: if(a_day==32){a_month++;a_day=1;} break;
   case 2: if(a_day==29){a_month++;a_day=1;} break;
   case 3:if(a_day==32){a_month++;a_day=1;} break;
   case 4: if(a_day==31){a_month++;a_day=1;} break;
   case 5:if(a_day==32){a_month++;a_day=1;} break;
   case 6: if(a_day==31){a_month++;a_day=1;} break;
   case 7:if(a_day==32){a_month++;a_day=1;} break;
   case 8:if(a_day==32){a_month++;a_day=1;} break;
   case 9: if(a_day==31){a_month++;a_day=1;} break;
   case 10:if(a_day==32){a_month++;a_day=1;} break;
   case 11:if(a_day==31){a_month++;a_day=1;} break;
   case 12:if(a_day==32){a_year++;a_month=1;a_day=1;} break;
   default: break;
   }
  

}







#define BUFF_SIZE 240*240 / 10 //lvgl屏幕初始化定义创建缓冲区大小
static lv_color_t buf_1[BUFF_SIZE];



//旋转编码器的回调函数
static void encoder_read(lv_indev_t * indev, lv_indev_data_t*data){

read_keys();
 if (tempKey == DOWN ) { 
    // key press is Down
   // do some stuff
   data->state = LV_INDEV_STATE_PRESSED;
   //data->state = LV_INDEV_STATE_RELEASED;

  }  
  if (tempKey == UP ) { 
     // key press is Up
    // do some stuff
  }  
  if (tempKey == RIGHT ) { 
     // key press is Right
    // do some stuff
    data->enc_diff = 1;
  } 
  if (tempKey == LEFT ) { 
     // key press is Left
    // do some stuff
    data->enc_diff = -1;

  }
  if (tempKey == -1 ) { 
    // no key press
    // do some stuff
  }

}
 




void setup() {
  // put your setup code here, to run once:

tian_wifi_init();//连接wifi
if(new_time()){_ui_label_set_property(ui_Label6,_UI_LABEL_PROPERTY_TEXT,String("OK").c_str());};//校准时间

//定时器初始化
// 配置周期性定时器
  timer.attach(1, flash, 1);//时间(秒)，回调，输入到回调函数的值

//get_ip();
//get_city();
get_tian_qi();//获取天气数据
//初始化LVGL 屏幕 
  lv_init();
  lv_display_t * disp;
  disp = lv_tft_espi_create(240, 240, buf_1, BUFF_SIZE);//屏幕宽，高，缓冲区，缓冲区大小
//创建旋转编码器为输入设备
  /*Register at least one display before you register any input devices*/
  static lv_indev_t * indev = lv_indev_create();
  lv_indev_set_type(indev, LV_INDEV_TYPE_ENCODER);   /*See below.*/
  lv_indev_set_read_cb(indev, encoder_read);  /*See below.*/  //绑定输入设备的回调函数
  lv_indev_read(indev);
  /* 创建group 后面将控件绑定到组即绑定到输入设备 */
  lv_group_t * group = lv_group_create();
  lv_group_set_default(group);//创建默认组，设备可操作ui控件
  lv_indev_set_group(indev, group);

   


ui_init();//squareline  UI  使用初始化






lv_tick_set_cb(millis);    //Arduino告知lvgl自启动以来过去了多少时间，很重要必须有，这样才能正常运行

  _ui_label_set_property(ui_Label2,_UI_LABEL_PROPERTY_TEXT,String(tempe).c_str());//向控件传输整数使其显示
  _ui_label_set_property(ui_Label1,_UI_LABEL_PROPERTY_TEXT,weather.c_str());//向控件传输String使其显示




}





////////////////////////////////////////////////////////////////////////////////////////////////


void citygett(lv_event_t * e)
{
	// Your code here
 
//lv_obj_t * dropdown = lv_event_get_target(e);
char buf[16];
lv_dropdown_get_selected_str(ui_Dropdown3, buf, sizeof(buf));//获取下拉框的选择字符

//city_value  = buf;
city = buf;
get_tian_qi();
_ui_label_set_property(ui_Label2,_UI_LABEL_PROPERTY_TEXT,String(tempe).c_str());
_ui_label_set_property(ui_Label1,_UI_LABEL_PROPERTY_TEXT,weather.c_str());

}

void button2call(lv_event_t * e)
{
	// Your code here
 if(new_time()){_ui_label_set_property(ui_Label6,_UI_LABEL_PROPERTY_TEXT,String("OK").c_str());}
 else{_ui_label_set_property(ui_Label6,_UI_LABEL_PROPERTY_TEXT,String("时间校准失败请检查网络连接").c_str());}

}

void button1call(lv_event_t * e)
{
	// Your code here
char buf[16];
lv_dropdown_get_selected_str(ui_Dropdown3, buf, sizeof(buf));//获取下拉框的选择字符

//city_value  = buf;
city = buf;
get_tian_qi();
_ui_label_set_property(ui_Label2,_UI_LABEL_PROPERTY_TEXT,String(tempe).c_str());
_ui_label_set_property(ui_Label1,_UI_LABEL_PROPERTY_TEXT,weather.c_str());

}


/////////////////////////////////////////////////////////////////////////////////////////////////






void loop() {
  // put your main code here, to run repeatedly:

 lv_timer_handler(); 
delay(5);
_ui_label_set_property(ui_Label4,_UI_LABEL_PROPERTY_TEXT,(String(a_year)+"年"+String(a_month)+"月"+String(a_day)+"日"+String(a_hour)+"时"+String(a_minute)+"分"+String(a_second)+"秒").c_str()  );

}
